// pages/common/distribution.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scope: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let invite_qr = wx.getStorageSync('invite_qr')
        if (invite_qr) {
            this.setData({
                invite_qr
            })
        } else {
            this.loadInviteQr()
        }
        this.loadData()
    },
    onShow() {
        this.power()
    },
    power() {
        wx.getSetting({
            success: res => {
                wx.authorize({
                    scope: 'scope.writePhotosAlbum',
                    success: res => {
                        console.log("授权成功");
                        this.setData({
                            scope: false
                        })
                    },
                    fail: err => {
                        this.setData({
                            scope: true
                        })
                    }
                })
            }
        })
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('rest/member/queryById.do').then(res => {
            this.setData({
                member: res.obj
            })
            wx.hideLoading()
        }).catch(err => {
            console.log(err)
        })

    },
    loadInviteQr() {
        app.http.post('rest/member/genAcodeun.do').then(res => {
            this.setData({
                invite_qr: res.obj,
            })
            wx.setStorage({
                key: 'invite_qr',
                data: res.obj,
            })
            wx.hideLoading()
        }).catch(err => {
            console.log(err)
        })
    },
    //点击开始的时间  
    timestart(e) {
        this.setData({
            timestart: e.timeStamp
        });
    },
    //点击结束的时间
    timeend(e) {
        this.setData({
            timeend: e.timeStamp
        });
    },
    //保存图片
    saveImg: function(e) {
        console.log(e)
        // let times = this.data.timeend - this.data.timestart;
        if (e.timeStamp > 500) {
            console.log("长按");
            let imgUrl = this.data.invite_qr
            wx.downloadFile({
                url: imgUrl,
                success: res => {
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success: res => {
                            wx.showToast({
                                title: '二维码保存成功',
                                icon: 'success'
                            })
                        },
                        fail: err => {
                            wx.showToast({
                                title: '保存失败，请稍后再试',
                                icon: 'none'
                            })
                            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                                this.setData({
                                    scope: true
                                })
                            }
                        }
                    })
                }
            })
        }
    },
    onPullDownRefresh() {
        this.loadData()
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: "车友友邀请您一起接单",
            path: "/pages/car-owner/index?scene=" + this.data.member.invite
        }
    }
})